<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Structure and Interpretation of Computer Programs, 2e: 1.1</title>

<meta name="description" content="Structure and Interpretation of Computer Programs, 2e: 1.1" />
<meta name="keywords" content="Structure and Interpretation of Computer Programs, 2e: 1.1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="Generator" content="texi2any" />
<meta charset="utf-8" />
<link href="index.xhtml#Top" rel="start" title="Top" />
<link href="Term-Index.xhtml#Term-Index" rel="index" title="Term Index" />
<link href="index.xhtml#SEC_Contents" rel="contents" title="Table of Contents" />
<link href="Chapter-1.xhtml#Chapter-1" rel="prev" title="Chapter 1" />
<link href="1_002e2.xhtml#g_t1_002e2" rel="next" title="1.2" />
<link href="Chapter-1.xhtml#Chapter-1" rel="prev" title="Chapter 1" />

<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/prettify.css" rel="stylesheet" type="text/css" />

<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/footnotes.js" type="text/javascript"></script>
<script src="js/browsertest.js" type="text/javascript"></script>
</head>

<body>
<section><span class="top jump" title="Jump to top"><a href="#pagetop" accesskey="t">⇡</a></span><a id="pagetop"></a><a id="g_t1_002e1"></a>
<nav class="header">
<p>
Next: <a href="1_002e2.xhtml#g_t1_002e2" accesskey="n" rel="next">1.2</a>, Prev: <a href="Chapter-1.xhtml#Chapter-1" accesskey="p" rel="prev">Chapter 1</a>, Up: <a href="Chapter-1.xhtml#Chapter-1" accesskey="u" rel="prev">Chapter 1</a>   [<a href="index.xhtml#SEC_Contents" title="Table of contents" accesskey="c" rel="contents">Contents</a>]</p>
</nav>
<a id="The-Elements-of-Programming"></a>
<h3 class="section"><span class="secnum">1.1</span><span class="sectitle">The Elements of Programming</span></h3>

<p>A powerful programming language is more than just a means for instructing a
computer to perform tasks.  The language also serves as a framework within
which we organize our ideas about processes.  Thus, when we describe a
language, we should pay particular attention to the means that the language
provides for combining simple ideas to form more complex ideas.  Every powerful
language has three mechanisms for accomplishing this:
</p>
<ul>
<li> <b>primitive expressions</b>,
which represent the simplest entities the language is concerned with,

</li><li> <b>means of combination</b>,
by which compound elements are built from simpler ones, and

</li><li> <b>means of abstraction</b>,
by which compound elements can be named and manipulated as units.

</li></ul>

<p>In programming, we deal with two kinds of elements: procedures and data. (Later
we will discover that they are really not so distinct.)  Informally, data is
“stuff” that we want to manipulate, and procedures are descriptions of the
rules for manipulating the data.  Thus, any powerful programming language
should be able to describe primitive data and primitive procedures and should
have methods for combining and abstracting procedures and data.
</p>
<p>In this chapter we will deal only with simple numerical data so that we can
focus on the rules for building procedures.<a class="footnote_link" id="DOCF4" href="#FOOT4"><sup>4</sup></a> In later chapters we will see that these
same rules allow us to build procedures to manipulate compound data as well.
</p>

<a id="g_t1_002e1_002e1"></a>
<a id="Expressions"></a>
<h4 class="subsection"><span class="secnum">1.1.1</span><span class="sectitle">Expressions</span></h4>

<p>One easy way to get started at programming is to examine some typical
interactions with an interpreter for the Scheme dialect of Lisp.  Imagine that
you are sitting at a computer terminal.  You type an <a id="index-expression"></a>
<em>expression</em>, and
the interpreter responds by displaying the result of its <a id="index-evaluating"></a>
<em>evaluating</em>
that expression.
</p>
<p>One kind of primitive expression you might type is a number.  (More precisely,
the expression that you type consists of the numerals that represent the number
in base 10.)  If you present Lisp with a number
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="lit">486</span></pre></div>

<p>the interpreter will respond by printing<a class="footnote_link" id="DOCF5" href="#FOOT5"><sup>5</sup></a>
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><i><span class="lit">486</span></i>
</pre></div>

<p>Expressions representing numbers may be combined with an expression
representing a primitive procedure (such as <code>+</code> or <code>*</code>) to form a
compound expression that represents the application of the procedure to those
numbers.  For example<!-- /@w -->:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">137</span><span class="pln"> </span><span class="lit">349</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">486</span></i><span class="pln">

</span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="lit">1000</span><span class="pln"> </span><span class="lit">334</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">666</span></i><span class="pln">

</span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">99</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">495</span></i><span class="pln">

</span><span class="opn">(</span><span class="pun">/</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">5</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">2</span></i><span class="pln">

</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">2.7</span><span class="pln"> </span><span class="lit">10</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">12.7</span></i>
</pre></div>

<p>Expressions such as these, formed by delimiting a list of expressions within
parentheses in order to denote procedure application, are called
<a id="index-combinations"></a>
<em>combinations</em>.  The leftmost element in the list is called the
<a id="index-operator"></a>
<em>operator</em>, and the other elements are called <a id="index-operands"></a>
<em>operands</em>.  The
value of a combination is obtained by applying the procedure specified by the
operator to the <a id="index-arguments"></a>
<em>arguments</em> that are the values of the operands.
</p>
<p>The convention of placing the operator to the left of the operands is known as
<a id="index-prefix-notation"></a>
<em>prefix notation</em>, and it may be somewhat confusing at first because it
departs significantly from the customary mathematical convention.  Prefix
notation has several advantages, however.  One of them is that it can
accommodate procedures that may take an arbitrary number of arguments, as in
the following examples:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">35</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="lit">7</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">75</span></i><span class="pln">

</span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">25</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="lit">12</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">1200</span></i>
</pre></div>

<p>No ambiguity can arise, because the operator is always the leftmost element and
the entire combination is delimited by the parentheses.
</p>
<p>A second advantage of prefix notation is that it extends in a straightforward
way to allow combinations to be <i>nested</i>, that is, to have combinations whose
elements are themselves combinations:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">5</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">6</span><span class="clo">))</span><span class="pln">
</span><i><span class="lit">19</span></i>
</pre></div>

<p>There is no limit (in principle) to the depth of such nesting and to the
overall complexity of the expressions that the Lisp interpreter can evaluate.
It is we humans who get confused by still relatively simple expressions such as
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">4</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">5</span><span class="clo">)))</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">7</span><span class="clo">)</span><span class="pln"> </span><span class="lit">6</span><span class="clo">))</span></pre></div>

<p>which the interpreter would readily evaluate to be 57.  We can help ourselves
by writing such an expression in the form
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">3</span><span class="pln">
      </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">4</span><span class="clo">)</span><span class="pln">
         </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">5</span><span class="clo">)))</span><span class="pln">
   </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">7</span><span class="clo">)</span><span class="pln">
      </span><span class="lit">6</span><span class="clo">))</span></pre></div>

<p>following a formatting convention known as <a id="index-pretty_002dprinting"></a>
<em>pretty-printing</em>, in which
each long combination is written so that the operands are aligned vertically.
The resulting indentations display clearly the structure of the
expression.<a class="footnote_link" id="DOCF6" href="#FOOT6"><sup>6</sup></a>
</p>
<p>Even with complex expressions, the interpreter always operates in the same
basic cycle: It reads an expression from the terminal, evaluates the
expression, and prints the result.  This mode of operation is often expressed
by saying that the interpreter runs in a <a id="index-read_002deval_002dprint-loop"></a>
<em>read-eval-print loop</em>.
Observe in particular that it is not necessary to explicitly instruct the
interpreter to print the value of the expression.<a class="footnote_link" id="DOCF7" href="#FOOT7"><sup>7</sup></a>
</p>
<a id="g_t1_002e1_002e2"></a>
<a id="Naming-and-the-Environment"></a>
<h4 class="subsection"><span class="secnum">1.1.2</span><span class="sectitle">Naming and the Environment</span></h4>

<p>A critical aspect of a programming language is the means it provides for using
names to refer to computational objects.  We say that the name identifies a
<a id="index-variable"></a>
<em>variable</em> whose <a id="index-value"></a>
<em>value</em> is the object.
</p>
<p>In the Scheme dialect of Lisp, we name things with <code>define</code>.  Typing
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> size </span><span class="lit">2</span><span class="clo">)</span></pre></div>

<p>causes the interpreter to associate the value 2 with the name
<code>size</code>.<a class="footnote_link" id="DOCF8" href="#FOOT8"><sup>8</sup></a> Once
the name <code>size</code> has been associated with the number 2, we can refer to the
value 2 by name:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="pln">size
</span><i><span class="lit">2</span></i><span class="pln">

</span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> size</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">10</span></i>
</pre></div>

<p>Here are further examples of the use of <code>define</code>:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> pi </span><span class="lit">3.14159</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> radius </span><span class="lit">10</span><span class="clo">)</span><span class="pln">

</span><span class="opn">(</span><span class="pun">*</span><span class="pln"> pi </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> radius radius</span><span class="clo">))</span><span class="pln">
</span><i><span class="lit">314.159</span></i><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> circumference </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> pi radius</span><span class="clo">))</span><span class="pln">

circumference
</span><i><span class="lit">62.8318</span></i>
</pre></div>

<p><code>Define</code> is our language’s simplest means of abstraction, for it allows us
to use simple names to refer to the results of compound operations, such as the
<code>circumference</code> computed above.  In general, computational objects may
have very complex structures, and it would be extremely inconvenient to have to
remember and repeat their details each time we want to use them.  Indeed,
complex programs are constructed by building, step by step, computational
objects of increasing complexity. The interpreter makes this step-by-step
program construction particularly convenient because name-object associations
can be created incrementally in successive interactions.  This feature
encourages the incremental development and testing of programs and is largely
responsible for the fact that a Lisp program usually consists of a large number
of relatively simple procedures.
</p>
<p>It should be clear that the possibility of associating values with symbols and
later retrieving them means that the interpreter must maintain some sort of
memory that keeps track of the name-object pairs.  This memory is called the
<a id="index-environment"></a>
<em>environment</em> (more precisely the <a id="index-global-environment"></a>
<em>global environment</em>, since
we will see later that a computation may involve a number of different
environments).<a class="footnote_link" id="DOCF9" href="#FOOT9"><sup>9</sup></a>
</p>
<a id="g_t1_002e1_002e3"></a>
<a id="Evaluating-Combinations"></a>
<h4 class="subsection"><span class="secnum">1.1.3</span><span class="sectitle">Evaluating Combinations</span></h4>

<p>One of our goals in this chapter is to isolate issues about thinking
procedurally.  As a case in point, let us consider that, in evaluating
combinations, the interpreter is itself following a procedure.
</p>
<blockquote>
<p>To evaluate a combination, do the following:
</p>
<ol>
<li> Evaluate the subexpressions of the combination.

</li><li> Apply the procedure that is the value of the leftmost subexpression (the
operator) to the arguments that are the values of the other subexpressions (the
operands).

</li></ol>
</blockquote>

<p>Even this simple rule illustrates some important points about processes in
general.  First, observe that the first step dictates that in order to
accomplish the evaluation process for a combination we must first perform the
evaluation process on each element of the combination.  Thus, the evaluation
rule is <a id="index-recursive"></a>
<em>recursive</em> in nature; that is, it includes, as one of its
steps, the need to invoke the rule itself.<a class="footnote_link" id="DOCF10" href="#FOOT10"><sup>10</sup></a>
</p>
<p>Notice how succinctly the idea of recursion can be used to express what, in the
case of a deeply nested combination, would otherwise be viewed as a rather
complicated process.  For example, evaluating
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="lit">6</span><span class="clo">))</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">7</span><span class="clo">))</span></pre></div>

<p>requires that the evaluation rule be applied to four different combinations.
We can obtain a picture of this process by representing the combination in the
form of a tree, as shown in <a href="#Figure-1_002e1">Figure 1.1</a>.  Each combination is represented
by a node with branches corresponding to the operator and the operands of the
combination stemming from it.  The terminal nodes (that is, nodes with no
branches stemming from them) represent either operators or numbers.  Viewing
evaluation in terms of the tree, we can imagine that the values of the operands
percolate upward, starting from the terminal nodes and then combining at higher
and higher levels.  In general, we shall see that recursion is a very powerful
technique for dealing with hierarchical, treelike objects.  In fact, the
“percolate values upward” form of the evaluation rule is an example of a
general kind of process known as <a id="index-tree-accumulation"></a>
<em>tree accumulation</em>.
</p>
<figure class="float">
<a id="Figure-1_002e1"></a>
<object style="width: 20.38ex; height: 20.46ex;" data="fig/chap1/Fig1.1g.std.svg" type="image/svg+xml">SVG</object>

<figcaption class="float-caption">
<p><strong>Figure 1.1:</strong> Tree representation, showing the value of each subcombination.</p>
</figcaption>
</figure>

<p>Next, observe that the repeated application of the first step brings us to the
point where we need to evaluate, not combinations, but primitive expressions
such as numerals, built-in operators, or other names.  We take care of the
primitive cases by stipulating that
</p>
<ul>
<li> the values of numerals are the numbers that they name,

</li><li> the values of built-in operators are the machine instruction sequences that
carry out the corresponding operations, and

</li><li> the values of other names are the objects associated with those names in the
environment.

</li></ul>

<p>We may regard the second rule as a special case of the third one by stipulating
that symbols such as <code>+</code> and <code>*</code> are also included in the global
environment, and are associated with the sequences of machine instructions that
are their “values.”  The key point to notice is the role of the environment
in determining the meaning of the symbols in expressions.  In an interactive
language such as Lisp, it is meaningless to speak of the value of an expression
such as <code>(+ x 1)</code> without specifying any information about the environment
that would provide a meaning for the symbol <code>x</code> (or even for the symbol
<code>+</code>).  As we shall see in <a href="Chapter-3.xhtml#Chapter-3">Chapter 3</a>, the general notion of the
environment as providing a context in which evaluation takes place will play an
important role in our understanding of program execution.
</p>
<p>Notice that the evaluation rule given above does not handle definitions.  For
instance, evaluating <code>(define x 3)</code> does not apply <code>define</code> to two
arguments, one of which is the value of the symbol <code>x</code> and the other of
which is 3, since the purpose of the <code>define</code> is precisely to associate
<code>x</code> with a value.  (That is, <code>(define x 3)</code> is not a combination.)
</p>
<p>Such exceptions to the general evaluation rule are called <a id="index-special-forms"></a>
<em>special forms</em>.  
<code>Define</code> is the only example of a special form that we have seen
so far, but we will meet others shortly.  Each special form has its own
evaluation rule. The various kinds of expressions (each with its associated
evaluation rule) constitute the syntax of the programming language.  In
comparison with most other programming languages, Lisp has a very simple
syntax; that is, the evaluation rule for expressions can be described by a
simple general rule together with specialized rules for a small number of
special forms.<a class="footnote_link" id="DOCF11" href="#FOOT11"><sup>11</sup></a>
</p>
<a id="g_t1_002e1_002e4"></a>
<a id="Compound-Procedures"></a>
<h4 class="subsection"><span class="secnum">1.1.4</span><span class="sectitle">Compound Procedures</span></h4>

<p>We have identified in Lisp some of the elements that must appear in any
powerful programming language:
</p>
<ul>
<li> Numbers and arithmetic operations are primitive data and procedures.

</li><li> Nesting of combinations provides a means of combining operations.

</li><li> Definitions that associate names with values provide a limited means of
abstraction.

</li></ul>

<p>Now we will learn about <a id="index-procedure-definitions"></a>
<em>procedure definitions</em>, a much more powerful
abstraction technique by which a compound operation can be given a name and
then referred to as a unit.
</p>
<p>We begin by examining how to express the idea of “squaring.”  We might say,
“To square something, multiply it by itself.”  This is expressed in our
language as
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square x</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> x x</span><span class="clo">))</span></pre></div>

<p>We can understand this in the following way:
</p>
<div class="example">
<pre class="example">(define (square x)    (*       x       x))
  |      |      |      |       |       |
 To square something, multiply it by itself.
</pre></div>

<p>We have here a <a id="index-compound-procedure"></a>
<em>compound procedure</em>, which has been given the name
<code>square</code>.  The procedure represents the operation of multiplying something
by itself.  The thing to be multiplied is given a local name, <code>x</code>, which
plays the same role that a pronoun plays in natural language.  Evaluating the
definition creates this compound procedure and associates it with the name
<code>square</code>.<a class="footnote_link" id="DOCF12" href="#FOOT12"><sup>12</sup></a>
</p>
<p>The general form of a procedure definition is
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">⟨</span><var><span class="pln">name</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">formal parameters</span></var><span class="pln">⟩</span><span class="clo">)</span><span class="pln"> ⟨</span><var><span class="pln">body</span></var><span class="pln">⟩</span><span class="clo">)</span></pre></div>

<p>The <code>⟨</code><var>name</var><code>⟩</code> is a symbol to be associated with the procedure definition in
the environment.<a class="footnote_link" id="DOCF13" href="#FOOT13"><sup>13</sup></a> The <code>⟨</code><var>formal<!-- /@w --> parameters<!-- /@w --></var><code>⟩</code> 
are the names used within the body of the procedure to refer to
the corresponding arguments of the procedure.  The <code>⟨</code><var>body</var><code>⟩</code> is an
expression that will yield the value of the procedure application when the
formal parameters are replaced by the actual arguments to which the procedure
is applied.<a class="footnote_link" id="DOCF14" href="#FOOT14"><sup>14</sup></a>  The <code>⟨</code><var>name</var><code>⟩</code> and
the <code>⟨</code><var>formal parameters</var><code>⟩</code> are grouped within parentheses, just as they
would be in an actual call to the procedure being defined.
</p>
<p>Having defined <code>square</code>, we can now use it:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">square </span><span class="lit">21</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">441</span></i><span class="pln">

</span><span class="opn">(</span><span class="pln">square </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">5</span><span class="clo">))</span><span class="pln">
</span><i><span class="lit">49</span></i><span class="pln">

</span><span class="opn">(</span><span class="pln">square </span><span class="opn">(</span><span class="pln">square </span><span class="lit">3</span><span class="clo">))</span><span class="pln">
</span><i><span class="lit">81</span></i>
</pre></div>

<p>We can also use <code>square</code> as a building block in defining other procedures.
For example, <math xmlns="http://www.w3.org/1998/Math/MathML">
  <msup>
    <mi>x</mi>
    <mn>2</mn>
  </msup>
  <mo>+</mo>
  <msup>
    <mi>y</mi>
    <mn>2</mn>
  </msup>
</math> can be expressed as
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square x</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square y</span><span class="clo">))</span></pre></div>

<p>We can easily define a procedure <code>sum-of-squares</code> that, given any two
numbers as arguments, produces the sum of their squares:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sum-of-squares x y</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square x</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square y</span><span class="clo">)))</span><span class="pln">

</span><span class="opn">(</span><span class="pln">sum-of-squares </span><span class="lit">3</span><span class="pln"> </span><span class="lit">4</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">25</span></i>
</pre></div>

<p>Now we can use <code>sum-of-squares</code> as a building block in constructing
further procedures:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">f a</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">sum-of-squares </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> a </span><span class="lit">1</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> a </span><span class="lit">2</span><span class="clo">)))</span><span class="pln">

</span><span class="opn">(</span><span class="pln">f </span><span class="lit">5</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">136</span></i>
</pre></div>

<p>Compound procedures are used in exactly the same way as primitive procedures.
Indeed, one could not tell by looking at the definition of
<code>sum-of-squares</code> given above whether <code>square</code> was built into the
interpreter, like <code>+</code> and <code>*</code>, or defined as a compound procedure.
</p>
<a id="g_t1_002e1_002e5"></a>
<a id="The-Substitution-Model-for-Procedure-Application"></a>
<h4 class="subsection"><span class="secnum">1.1.5</span><span class="sectitle">The Substitution Model for Procedure Application</span></h4>

<p>To evaluate a combination whose operator names a compound procedure, the
interpreter follows much the same process as for combinations whose operators
name primitive procedures, which we described in <a href="#g_t1_002e1_002e3">1.1.3</a>.  That is,
the interpreter evaluates the elements of the combination and applies the
procedure (which is the value of the operator of the combination) to the
arguments (which are the values of the operands of the combination).
</p>
<p>We can assume that the mechanism for applying primitive procedures to arguments
is built into the interpreter.  For compound procedures, the application
process is as follows:
</p>
<blockquote>
<p>To apply a compound procedure to arguments, evaluate the body of the procedure
with each formal parameter replaced by the corresponding argument.
</p></blockquote>

<p>To illustrate this process, let’s evaluate the combination
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">f </span><span class="lit">5</span><span class="clo">)</span></pre></div>

<p>where <code>f</code> is the procedure defined in <a href="#g_t1_002e1_002e4">1.1.4</a>.  We begin by
retrieving the body of <code>f</code>:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">sum-of-squares </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> a </span><span class="lit">1</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> a </span><span class="lit">2</span><span class="clo">))</span></pre></div>

<p>Then we replace the formal parameter <code>a</code> by the argument 5:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">sum-of-squares </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">1</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">2</span><span class="clo">))</span></pre></div>

<p>Thus the problem reduces to the evaluation of a combination with two operands
and an operator <code>sum-of-squares</code>.  Evaluating this combination involves
three subproblems.  We must evaluate the operator to get the procedure to be
applied, and we must evaluate the operands to get the arguments.  Now <code>(+
5 1)</code> produces 6 and <code>(* 5 2)</code> produces 10, so we must apply the
<code>sum-of-squares</code> procedure to 6 and 10.  These values are substituted for
the formal parameters <code>x</code> and <code>y</code> in the body of
<code>sum-of-squares</code>, reducing the expression to
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square </span><span class="lit">6</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square </span><span class="lit">10</span><span class="clo">))</span></pre></div>

<p>If we use the definition of <code>square</code>, this reduces to
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="lit">6</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">10</span><span class="clo">))</span></pre></div>

<p>which reduces by multiplication to
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">36</span><span class="pln"> </span><span class="lit">100</span><span class="clo">)</span></pre></div>

<p>and finally to
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="lit">136</span></pre></div>

<p>The process we have just described is called the <a id="index-substitution-model"></a>
<em>substitution model</em>
for procedure application.  It can be taken as a model that determines the
“meaning” of procedure application, insofar as the procedures in this chapter
are concerned.  However, there are two points that should be stressed:
</p>
<ul>
<li> The purpose of the substitution is to help us think about procedure
application, not to provide a description of how the interpreter really works.
Typical interpreters do not evaluate procedure applications by manipulating the
text of a procedure to substitute values for the formal parameters.  In
practice, the “substitution” is accomplished by using a local environment for
the formal parameters.  We will discuss this more fully in <a href="Chapter-3.xhtml#Chapter-3">Chapter 3</a> and
<a href="Chapter-4.xhtml#Chapter-4">Chapter 4</a> when we examine the implementation of an interpreter in detail.

</li><li> Over the course of this book, we will present a sequence of increasingly
elaborate models of how interpreters work, culminating with a complete
implementation of an interpreter and compiler in <a href="Chapter-5.xhtml#Chapter-5">Chapter 5</a>.  The
substitution model is only the first of these models—a way to get started
thinking formally about the evaluation process.  In general, when modeling
phenomena in science and engineering, we begin with simplified, incomplete
models.  As we examine things in greater detail, these simple models become
inadequate and must be replaced by more refined models.  The substitution model
is no exception.  In particular, when we address in <a href="Chapter-3.xhtml#Chapter-3">Chapter 3</a> the use of
procedures with “mutable data,” we will see that the substitution model
breaks down and must be replaced by a more complicated model of procedure
application.<a class="footnote_link" id="DOCF15" href="#FOOT15"><sup>15</sup></a>

</li></ul>

<a id="Applicative-order-versus-normal-order"></a>
<h5 class="subsubheading">Applicative order versus normal order</h5>

<p>According to the description of evaluation given in <a href="#g_t1_002e1_002e3">1.1.3</a>, the
interpreter first evaluates the operator and operands and then applies the
resulting procedure to the resulting arguments.  This is not the only way to
perform evaluation.  An alternative evaluation model would not evaluate the
operands until their values were needed.  Instead it would first substitute
operand expressions for parameters until it obtained an expression involving
only primitive operators, and would then perform the evaluation.  If we used
this method, the evaluation of <code>(f 5)</code> would proceed according to the
sequence of expansions
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">sum-of-squares </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">1</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">2</span><span class="clo">))</span><span class="pln">

</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">1</span><span class="clo">))</span><span class="pln"> 
   </span><span class="opn">(</span><span class="pln">square </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">2</span><span class="clo">)))</span><span class="pln">

</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">1</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">1</span><span class="clo">))</span><span class="pln"> 
   </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">2</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">2</span><span class="clo">)))</span></pre></div>

<p>followed by the reductions
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="lit">6</span><span class="clo">)</span><span class="pln"> 
   </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="lit">10</span><span class="clo">))</span><span class="pln">

</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">36</span><span class="pln"> </span><span class="lit">100</span><span class="clo">)</span><span class="pln">

</span><span class="lit">136</span></pre></div>

<p>This gives the same answer as our previous evaluation model, but the process is
different.  In particular, the evaluations of <code>(+ 5 1)</code> and <code>(* 5 2)</code>
are each performed twice here, corresponding to the reduction of the expression
<code>(* x x)</code> with <code>x</code> replaced respectively by <code>(+ 5 1)</code> and
<code>(* 5 2)</code>.
</p>
<p>This alternative “fully expand and then reduce” evaluation method is known as
<a id="index-normal_002dorder-evaluation"></a>
<em>normal-order evaluation</em>, in contrast to the “evaluate the arguments
and then apply” method that the interpreter actually uses, which is called
<a id="index-applicative_002dorder-evaluation"></a>
<em>applicative-order evaluation</em>.  It can be shown that, for procedure
applications that can be modeled using substitution (including all the
procedures in the first two chapters of this book) and that yield legitimate
values, normal-order and applicative-order evaluation produce the same value.
(See <a href="#Exercise-1_002e5">Exercise 1.5</a> for an instance of an “illegitimate” value where
normal-order and applicative-order evaluation do not give the same result.)
</p>
<p>Lisp uses applicative-order evaluation, partly because of the additional
efficiency obtained from avoiding multiple evaluations of expressions such as
those illustrated with <code>(+ 5 1)</code> and <code>(* 5 2)</code> above and, more
significantly, because normal-order evaluation becomes much more complicated to
deal with when we leave the realm of procedures that can be modeled by
substitution.  On the other hand, normal-order evaluation can be an extremely
valuable tool, and we will investigate some of its implications in <a href="Chapter-3.xhtml#Chapter-3">Chapter 3</a> and <a href="Chapter-4.xhtml#Chapter-4">Chapter 4</a>.<a class="footnote_link" id="DOCF16" href="#FOOT16"><sup>16</sup></a>
</p>
<a id="g_t1_002e1_002e6"></a>
<a id="Conditional-Expressions-and-Predicates"></a>
<h4 class="subsection"><span class="secnum">1.1.6</span><span class="sectitle">Conditional Expressions and Predicates</span></h4>

<p>The expressive power of the class of procedures that we can define at this
point is very limited, because we have no way to make tests and to perform
different operations depending on the result of a test.  For instance, we
cannot define a procedure that computes the absolute value of a number by
testing whether the number is positive, negative, or zero and taking different
actions in the different cases according to the rule

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <mrow>
    <mo>|</mo>
    <mrow class="MJX-TeXAtom-ORD">
      <mi>x</mi>
    </mrow>
    <mo>|</mo>
  </mrow>
  <mspace width="thickmathspace"/>
  <mo>=</mo>
  <mspace width="thickmathspace"/>
  <mrow>
    <mo>{</mo>
    <mtable columnalign="right left left" rowspacing="4pt" columnspacing="1em">
      <mtr>
        <mtd>
          <mi>x</mi>
        </mtd>
        <mtd>
          <mspace width="thickmathspace"/>
          <mtext>if</mtext>
        </mtd>
        <mtd>
          <mi>x</mi>
          <mo>&gt;<!-- > --></mo>
          <mn>0</mn>
          <mo>,</mo>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>0</mn>
        </mtd>
        <mtd>
          <mspace width="thickmathspace"/>
          <mtext>if</mtext>
        </mtd>
        <mtd>
          <mi>x</mi>
          <mo>=</mo>
          <mn>0</mn>
          <mo>,</mo>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mo>−<!-- − --></mo>
          <mi>x</mi>
        </mtd>
        <mtd>
          <mspace width="thickmathspace"/>
          <mtext>if</mtext>
        </mtd>
        <mtd>
          <mi>x</mi>
          <mo>&lt;<!-- < --></mo>
          <mn>0.</mn>
        </mtd>
      </mtr>
    </mtable>
  </mrow>
</math>

This construct is called a <a id="index-case-analysis"></a>
<em>case analysis</em>, and there is a special form
in Lisp for notating such a case analysis.  It is called <code>cond</code> (which
stands for “conditional”), and it is used as follows:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">cond</span><span class="pln"> </span><span class="opn">((</span><span class="pun">&gt;</span><span class="pln"> x </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">)</span><span class="pln">
        </span><span class="opn">((</span><span class="pun">=</span><span class="pln"> x </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> </span><span class="lit">0</span><span class="clo">)</span><span class="pln">
        </span><span class="opn">((</span><span class="pun">&lt;</span><span class="pln"> x </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> x</span><span class="clo">))))</span></pre></div>

<p>The general form of a conditional expression is
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">cond</span><span class="pln"> </span><span class="opn">(</span><span class="pln">⟨</span><var><span class="pln">p</span><span class="pun">₁</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">e</span><span class="pun">₁</span></var><span class="pln">⟩</span><span class="clo">)</span><span class="pln">
      </span><span class="opn">(</span><span class="pln">⟨</span><var><span class="pln">p</span><span class="pun">₂</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">e</span><span class="pun">₂</span></var><span class="pln">⟩</span><span class="clo">)</span><span class="pln">
      </span><span class="roman"><span class="pun">…</span></span><span class="pln">
      </span><span class="opn">(</span><span class="pln">⟨</span><var><span class="pln">p</span><span class="pun">ₙ</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">e</span><span class="pun">ₙ</span></var><span class="pln">⟩</span><span class="clo">))</span></pre></div>

<p>consisting of the symbol <code>cond</code> followed by parenthesized pairs of
expressions
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">⟨</span><var><span class="pln">p</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">e</span></var><span class="pln">⟩</span><span class="clo">)</span></pre></div>

<p>called <a id="index-clauses"></a>
<em>clauses</em>. The first expression in each pair is a
<a id="index-predicate"></a>
<em>predicate</em>—that is, an expression whose value is interpreted as
either true or false.<a class="footnote_link" id="DOCF17" href="#FOOT17"><sup>17</sup></a>
</p>
<p>Conditional expressions are evaluated as follows.  The predicate <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <msub>
      <mi>p</mi>
      <mn>1</mn>
    </msub>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math> is
evaluated first.  If its value is false, then <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <msub>
      <mi>p</mi>
      <mn>2</mn>
    </msub>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math> is evaluated.  If
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <msub>
      <mi>p</mi>
      <mn>2</mn>
    </msub>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math>’s value is also false, then <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <msub>
      <mi>p</mi>
      <mn>3</mn>
    </msub>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math> is evaluated.  This process
continues until a predicate is found whose value is true, in which case the
interpreter returns the value of the corresponding <a id="index-consequent-expression"></a>
<em>consequent expression</em> 
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <mi>e</mi>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math> of the clause as the value of the conditional expression.
If none of the <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <mi>p</mi>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math>’s is found to be true, the value of the <code>cond</code> is
undefined.
</p>
<p>The word <a id="index-predicate-1"></a>
<em>predicate</em> is used for procedures that return true or false,
as well as for expressions that evaluate to true or false.  The absolute-value
procedure <code>abs</code> makes use of the primitive predicates <code>&gt;</code>, <code>&lt;</code>,
and <code>=</code>.<a class="footnote_link" id="DOCF18" href="#FOOT18"><sup>18</sup></a> These take two numbers as arguments and test whether the first
number is, respectively, greater than, less than, or equal to the second
number, returning true or false accordingly.
</p>
<p>Another way to write the absolute-value procedure is
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">cond</span><span class="pln"> </span><span class="opn">((</span><span class="pun">&lt;</span><span class="pln"> x </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> x</span><span class="clo">))</span><span class="pln">
        </span><span class="opn">(</span><span class="kwd">else</span><span class="pln"> x</span><span class="clo">)))</span></pre></div>

<p>which could be expressed in English as “If <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi>
</math> is less than zero return 
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo>−<!-- − --></mo>
    <mi>x</mi>
  </mrow>
</math>; otherwise return <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi>
</math>.”  <code>Else</code> is a special symbol that can be
used in place of the <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <mi>p</mi>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math> in the final clause of a <code>cond</code>.  This
causes the <code>cond</code> to return as its value the value of the corresponding
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <mi>e</mi>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math> whenever all previous clauses have been bypassed.  In fact, any
expression that always evaluates to a true value could be used as the <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mo stretchy="false">⟨</mo>
    <mi>p</mi>
    <mo stretchy="false">⟩</mo>
  </mrow>
</math>
here.
</p>
<p>Here is yet another way to write the absolute-value procedure:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> x </span><span class="lit">0</span><span class="clo">)</span><span class="pln">
      </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> x</span><span class="clo">)</span><span class="pln">
      x</span><span class="clo">))</span></pre></div>

<p>This uses the special form <code>if</code>, a restricted type of conditional that can
be used when there are precisely two cases in the case analysis.  The general
form of an <code>if</code> expression is
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">if</span><span class="pln"> ⟨</span><var><span class="pln">predicate</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">consequent</span></var><span class="pln">⟩ ⟨</span><var><span class="pln">alternative</span></var><span class="pln">⟩</span><span class="clo">)</span></pre></div>

<p>To evaluate an <code>if</code> expression, the interpreter starts by evaluating the
<code>⟨</code><var>predicate</var><code>⟩</code> part of the expression.  If the <code>⟨</code><var>predicate</var><code>⟩</code> evaluates
to a true value, the interpreter then evaluates the <code>⟨</code><var>consequent</var><code>⟩</code> and
returns its value.  Otherwise it evaluates the <code>⟨</code><var>alternative</var><code>⟩</code> and returns
its value.<a class="footnote_link" id="DOCF19" href="#FOOT19"><sup>19</sup></a>
</p>
<p>In addition to primitive predicates such as <code>&lt;</code>, <code>=</code>, and <code>&gt;</code>,
there are logical composition operations, which enable us to construct compound
predicates.  The three most frequently used are these:
</p>
<ul>
<li> <code>(and ⟨<var>e₁</var>⟩ <span class="roman">…</span> ⟨<var>eₙ</var>⟩)</code>

<p>The interpreter evaluates the expressions <code>⟨</code><var>e</var><code>⟩</code> one at a time, in
left-to-right order.  If any <code>⟨</code><var>e</var><code>⟩</code> evaluates to false, the value of the
<code>and</code> expression is false, and the rest of the <code>⟨</code><var>e</var><code>⟩</code>’s are not
evaluated.  If all <code>⟨</code><var>e</var><code>⟩</code>’s evaluate to true values, the value of the
<code>and</code> expression is the value of the last one.
</p>
</li><li> <code>(or ⟨<var>e₁</var>⟩ <span class="roman">…</span> ⟨<var>eₙ</var>⟩)</code>

<p>The interpreter evaluates the expressions <code>⟨</code><var>e</var><code>⟩</code> one at a time, in
left-to-right order.  If any <code>⟨</code><var>e</var><code>⟩</code> evaluates to a true value, that value is
returned as the value of the <code>or</code> expression, and the rest of the
<code>⟨</code><var>e</var><code>⟩</code>’s are not evaluated.  If all <code>⟨</code><var>e</var><code>⟩</code>’s evaluate to false, the value
of the <code>or</code> expression is false.
</p>
</li><li> <code>(not ⟨<var>e</var>⟩)</code>

<p>The value of a <code>not</code> expression is true when the expression <code>⟨</code><var>e</var><code>⟩</code>
evaluates to false, and false otherwise.
</p>
</li></ul>

<p>Notice that <code>and</code> and <code>or</code> are special forms, not procedures, because
the subexpressions are not necessarily all evaluated.  <code>Not</code> is an
ordinary procedure.
</p>
<p>As an example of how these are used, the condition that a number <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi>
</math> be in
the range <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mn>5</mn>
  <mo>&lt;</mo>
  <mi>x</mi>
  <mo>&lt;</mo>
  <mn>10</mn>
</math> may be expressed as
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">and</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;</span><span class="pln"> x </span><span class="lit">5</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> x </span><span class="lit">10</span><span class="clo">))</span></pre></div>

<p>As another example, we can define a predicate to test whether one number is
greater than or equal to another as 
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;=</span><span class="pln"> x y</span><span class="clo">)</span><span class="pln"> 
  </span><span class="opn">(</span><span class="kwd">or</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;</span><span class="pln"> x y</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">=</span><span class="pln"> x y</span><span class="clo">)))</span></pre></div>

<p>or alternatively as 
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;=</span><span class="pln"> x y</span><span class="clo">)</span><span class="pln"> 
  </span><span class="opn">(</span><span class="pln">not </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> x y</span><span class="clo">)))</span></pre></div>

<blockquote>
<p><strong><a id="Exercise-1_002e1"></a>Exercise 1.1:</strong> Below is a sequence of expressions.
What is the result printed by the interpreter in response to each expression?
Assume that the sequence is to be evaluated in the order in which it is
presented.
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="lit">10</span><span class="pln">
</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">4</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="lit">9</span><span class="pln"> </span><span class="lit">1</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="pun">/</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="lit">2</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">4</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="lit">6</span><span class="clo">))</span><span class="pln">
</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> a </span><span class="lit">3</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> b </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> a </span><span class="lit">1</span><span class="clo">))</span><span class="pln">
</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> a b </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> a b</span><span class="clo">))</span><span class="pln">
</span><span class="opn">(</span><span class="pun">=</span><span class="pln"> a b</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="kwd">and</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;</span><span class="pln"> b a</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> b </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> a b</span><span class="clo">)))</span><span class="pln">
    b
    a</span><span class="clo">)</span><span class="pln">
</span><span class="opn">(</span><span class="kwd">cond</span><span class="pln"> </span><span class="opn">((</span><span class="pun">=</span><span class="pln"> a </span><span class="lit">4</span><span class="clo">)</span><span class="pln"> </span><span class="lit">6</span><span class="clo">)</span><span class="pln">
      </span><span class="opn">((</span><span class="pun">=</span><span class="pln"> b </span><span class="lit">4</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">6</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> a</span><span class="clo">))</span><span class="pln">
      </span><span class="opn">(</span><span class="kwd">else</span><span class="pln"> </span><span class="lit">25</span><span class="clo">))</span><span class="pln">
</span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;</span><span class="pln"> b a</span><span class="clo">)</span><span class="pln"> b a</span><span class="clo">))</span><span class="pln">
</span><span class="opn">(</span><span class="pun">*</span><span class="pln"> </span><span class="opn">(</span><span class="kwd">cond</span><span class="pln"> </span><span class="opn">((</span><span class="pun">&gt;</span><span class="pln"> a b</span><span class="clo">)</span><span class="pln"> a</span><span class="clo">)</span><span class="pln">
         </span><span class="opn">((</span><span class="pun">&lt;</span><span class="pln"> a b</span><span class="clo">)</span><span class="pln"> b</span><span class="clo">)</span><span class="pln">
         </span><span class="opn">(</span><span class="kwd">else</span><span class="pln"> </span><span class="lit">-1</span><span class="clo">))</span><span class="pln">
   </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> a </span><span class="lit">1</span><span class="clo">))</span></pre></div>
</blockquote>

<blockquote>
<p><strong><a id="Exercise-1_002e2"></a>Exercise 1.2:</strong> Translate the following expression
into prefix form:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <mrow class="MJX-TeXAtom-ORD">
    <mfrac>
      <mrow>
        <mn>5</mn>
        <mo>+</mo>
        <mn>4</mn>
        <mo>+</mo>
        <mo stretchy="false">(</mo>
        <mn>2</mn>
        <mo>−<!-- − --></mo>
        <mo stretchy="false">(</mo>
        <mn>3</mn>
        <mo>−<!-- − --></mo>
        <mo stretchy="false">(</mo>
        <mn>6</mn>
        <mo>+</mo>
        <mfrac>
          <mn>4</mn>
          <mn>5</mn>
        </mfrac>
        <mo stretchy="false">)</mo>
        <mo stretchy="false">)</mo>
        <mo stretchy="false">)</mo>
      </mrow>
      <mrow>
        <mn>3</mn>
        <mo stretchy="false">(</mo>
        <mn>6</mn>
        <mo>−<!-- − --></mo>
        <mn>2</mn>
        <mo stretchy="false">)</mo>
        <mo stretchy="false">(</mo>
        <mn>2</mn>
        <mo>−<!-- − --></mo>
        <mn>7</mn>
        <mo stretchy="false">)</mo>
      </mrow>
    </mfrac>
    <mo>.</mo>
  </mrow>
</math>

</p></blockquote>

<blockquote>
<p><strong><a id="Exercise-1_002e3"></a>Exercise 1.3:</strong> Define a procedure that takes three
numbers as arguments and returns the sum of the squares of the two larger
numbers.
</p></blockquote>

<blockquote>
<p><strong><a id="Exercise-1_002e4"></a>Exercise 1.4:</strong> Observe that our model of
evaluation allows for combinations whose operators are compound expressions.
Use this observation to describe the behavior of the following procedure:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">a-plus-abs-b a b</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">((</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;</span><span class="pln"> b </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">-</span><span class="clo">)</span><span class="pln"> a b</span><span class="clo">))</span></pre></div>
</blockquote>

<blockquote>
<p><strong><a id="Exercise-1_002e5"></a>Exercise 1.5:</strong> Ben Bitdiddle has invented a test
to determine whether the interpreter he is faced with is using
applicative-order evaluation or normal-order evaluation.  He defines the
following two procedures:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">p</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pln">p</span><span class="clo">))</span><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">test x y</span><span class="clo">)</span><span class="pln"> 
  </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pun">=</span><span class="pln"> x </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> 
      </span><span class="lit">0</span><span class="pln"> 
      y</span><span class="clo">))</span></pre></div>

<p>Then he evaluates the expression
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">test </span><span class="lit">0</span><span class="pln"> </span><span class="opn">(</span><span class="pln">p</span><span class="clo">))</span></pre></div>

<p>What behavior will Ben observe with an interpreter that uses applicative-order
evaluation?  What behavior will he observe with an interpreter that uses
normal-order evaluation?  Explain your answer.  (Assume that the evaluation
rule for the special form <code>if</code> is the same whether the interpreter is
using normal or applicative order: The predicate expression is evaluated first,
and the result determines whether to evaluate the consequent or the alternative
expression.)
</p></blockquote>

<a id="Sec_002e1_002e1_002e7"></a><a id="g_t1_002e1_002e7"></a>
<a id="Example_003a-Square-Roots-by-Newton_0027s-Method"></a>
<h4 class="subsection"><span class="secnum">1.1.7</span><span class="sectitle">Example: Square Roots by Newton’s Method</span></h4>

<p>Procedures, as introduced above, are much like ordinary mathematical functions.
They specify a value that is determined by one or more parameters.  But there
is an important difference between mathematical functions and computer
procedures.  Procedures must be effective.
</p>
<p>As a case in point, consider the problem of computing square roots.  We can
define the square-root function as

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <msqrt>
    <mi>x</mi>
  </msqrt>
  <mspace width="thickmathspace"/>
  <mspace width="thickmathspace"/>
  <mo>=</mo>
  <mspace width="thickmathspace"/>
  <mspace width="thickmathspace"/>
  <mrow class="MJX-TeXAtom-ORD">
    <mtext>the</mtext>
    <mspace width="thickmathspace"/>
    <mspace width="thickmathspace"/>
    <mi>y</mi>
  </mrow>
  <mspace width="thickmathspace"/>
  <mspace width="thickmathspace"/>
  <mrow class="MJX-TeXAtom-ORD">
    <mtext>such that</mtext>
  </mrow>
  <mspace width="thickmathspace"/>
  <mspace width="thickmathspace"/>
  <mrow class="MJX-TeXAtom-ORD">
    <mi>y</mi>
    <mo>≥<!-- ≥ --></mo>
    <mn>0</mn>
  </mrow>
  <mspace width="thickmathspace"/>
  <mspace width="thickmathspace"/>
  <mrow class="MJX-TeXAtom-ORD">
    <mtext>and</mtext>
    <mspace width="thickmathspace"/>
    <mspace width="thickmathspace"/>
    <msup>
      <mi>y</mi>
      <mn>2</mn>
    </msup>
    <mo>=</mo>
    <mi>x</mi>
    <mo>.</mo>
  </mrow>
</math>

This describes a perfectly legitimate mathematical function.  We could use it
to recognize whether one number is the square root of another, or to derive
facts about square roots in general.  On the other hand, the definition does
not describe a procedure.  Indeed, it tells us almost nothing about how to
actually find the square root of a given number.  It will not help matters to
rephrase this definition in pseudo-Lisp:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">the y </span><span class="opn">(</span><span class="kwd">and</span><span class="pln"> </span><span class="opn">(</span><span class="pun">&gt;=</span><span class="pln"> y </span><span class="lit">0</span><span class="clo">)</span><span class="pln"> 
              </span><span class="opn">(</span><span class="pun">=</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square y</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">))))</span></pre></div>

<p>This only begs the question.
</p>
<p>The contrast between function and procedure is a reflection of the general
distinction between describing properties of things and describing how to do
things, or, as it is sometimes referred to, the distinction between declarative
knowledge and imperative knowledge.  In mathematics we are usually concerned
with declarative (what is) descriptions, whereas in computer science we are
usually concerned with imperative (how to) descriptions.<a class="footnote_link" id="DOCF20" href="#FOOT20"><sup>20</sup></a>
</p>
<p>How does one compute square roots?  The most common way is to use Newton’s
method of successive approximations, which says that whenever we have a guess
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>y</mi>
</math> for the value of the square root of a number <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi>
</math>, we can perform a
simple manipulation to get a better guess (one closer to the actual square
root) by averaging <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>y</mi>
</math> with <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow class="MJX-TeXAtom-ORD">
    <mi>x</mi>
    <mrow class="MJX-TeXAtom-ORD">
      <mo>/</mo>
    </mrow>
    <mi>y</mi>
  </mrow>
</math>.<a class="footnote_link" id="DOCF21" href="#FOOT21"><sup>21</sup></a> 
For example, we can compute the square root of 2 as follows.
Suppose our initial guess is 1:
</p>
<div class="example">
<pre class="example">Guess     Quotient      Average

1         (2/1)  = 2    ((2 + 1)/2)  = 1.5

1.5       (2/1.5)       ((1.3333 + 1.5)/2)
            = 1.3333      = 1.4167

1.4167    (2/1.4167)    ((1.4167 + 1.4118)/2) 
            = 1.4118      = 1.4142  

1.4142    ...           ...
</pre></div>

<p>Continuing this process, we obtain better and better approximations to the
square root.
</p>
<p>Now let’s formalize the process in terms of procedures.  We start with a value
for the radicand (the number whose square root we are trying to compute) and a
value for the guess.  If the guess is good enough for our purposes, we are
done; if not, we must repeat the process with an improved guess.  We write this
basic strategy as a procedure:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt-iter guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
      guess
      </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">)))</span></pre></div>

<p>A guess is improved by averaging it with the quotient of the radicand and the
old guess:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">average guess </span><span class="opn">(</span><span class="pun">/</span><span class="pln"> x guess</span><span class="clo">)))</span></pre></div>

<p>where
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">average x y</span><span class="clo">)</span><span class="pln"> 
  </span><span class="opn">(</span><span class="pun">/</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> x y</span><span class="clo">)</span><span class="pln"> </span><span class="lit">2</span><span class="clo">))</span></pre></div>

<p>We also have to say what we mean by “good enough.”  The following will do for
illustration, but it is not really a very good test.  (See <a href="#Exercise-1_002e7">Exercise 1.7</a>.)  
The idea is to improve the answer until it is close
enough so that its square differs from the radicand by less than a
predetermined tolerance (here 0.001):<a class="footnote_link" id="DOCF22" href="#FOOT22"><sup>22</sup></a>
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square guess</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">))</span><span class="pln"> </span><span class="lit">0.001</span><span class="clo">))</span></pre></div>

<p>Finally, we need a way to get started.  For instance, we can always guess that
the square root of any number is 1:<a class="footnote_link" id="DOCF23" href="#FOOT23"><sup>23</sup></a>
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="lit">1.0</span><span class="pln"> x</span><span class="clo">))</span></pre></div>

<p>If we type these definitions to the interpreter, we can use <code>sqrt</code> just as
we can use any procedure:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">sqrt </span><span class="lit">9</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">3.00009155413138</span></i><span class="pln">

</span><span class="opn">(</span><span class="pln">sqrt </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="lit">100</span><span class="pln"> </span><span class="lit">37</span><span class="clo">))</span><span class="pln">
</span><i><span class="lit">11.704699917758145</span></i><span class="pln">

</span><span class="opn">(</span><span class="pln">sqrt </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt </span><span class="lit">2</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt </span><span class="lit">3</span><span class="clo">)))</span><span class="pln">
</span><i><span class="lit">1.7739279023207892</span></i><span class="pln">

</span><span class="opn">(</span><span class="pln">square </span><span class="opn">(</span><span class="pln">sqrt </span><span class="lit">1000</span><span class="clo">))</span><span class="pln">
</span><i><span class="lit">1000.000369924366</span></i>
</pre></div>

<p>The <code>sqrt</code> program also illustrates that the simple procedural language we
have introduced so far is sufficient for writing any purely numerical program
that one could write in, say, C or Pascal.  This might seem surprising, since
we have not included in our language any iterative (looping) constructs that
direct the computer to do something over and over again.  <code>Sqrt-iter</code>, on
the other hand, demonstrates how iteration can be accomplished using no special
construct other than the ordinary ability to call a procedure.<a class="footnote_link" id="DOCF24" href="#FOOT24"><sup>24</sup></a>
</p>
<blockquote>
<p><strong><a id="Exercise-1_002e6"></a>Exercise 1.6:</strong> Alyssa P. Hacker doesn’t see why
<code>if</code> needs to be provided as a special form.  “Why can’t I just define it
as an ordinary procedure in terms of <code>cond</code>?” she asks.  Alyssa’s friend
Eva Lu Ator claims this can indeed be done, and she defines a new version of
<code>if</code>:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">new-if predicate 
                then-clause 
                else-clause</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">cond</span><span class="pln"> </span><span class="opn">(</span><span class="pln">predicate then-clause</span><span class="clo">)</span><span class="pln">
        </span><span class="opn">(</span><span class="kwd">else</span><span class="pln"> else-clause</span><span class="clo">)))</span></pre></div>

<p>Eva demonstrates the program for Alyssa:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="pln">new-if </span><span class="opn">(</span><span class="pun">=</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">3</span><span class="clo">)</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">5</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">5</span></i><span class="pln">

</span><span class="opn">(</span><span class="pln">new-if </span><span class="opn">(</span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span class="lit">1</span><span class="clo">)</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="lit">5</span><span class="clo">)</span><span class="pln">
</span><i><span class="lit">0</span></i>
</pre></div>

<p>Delighted, Alyssa uses <code>new-if</code> to rewrite the square-root program:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt-iter guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">new-if </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
          guess
          </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">)))</span></pre></div>

<p>What happens when Alyssa attempts to use this to compute square roots?
Explain.
</p></blockquote>

<blockquote>
<p><strong><a id="Exercise-1_002e7"></a>Exercise 1.7:</strong> The <code>good-enough?</code> test used
in computing square roots will not be very effective for finding the square
roots of very small numbers.  Also, in real computers, arithmetic operations
are almost always performed with limited precision.  This makes our test
inadequate for very large numbers.  Explain these statements, with examples
showing how the test fails for small and large numbers.  An alternative
strategy for implementing <code>good-enough?</code> is to watch how <code>guess</code>
changes from one iteration to the next and to stop when the change is a very
small fraction of the guess.  Design a square-root procedure that uses this
kind of end test.  Does this work better for small and large numbers?
</p></blockquote>

<blockquote>
<p><strong><a id="Exercise-1_002e8"></a>Exercise 1.8:</strong> Newton’s method for cube roots is
based on the fact that if <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>y</mi>
</math> is an approximation to the cube root of <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi>
</math>,
then a better approximation is given by the value

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <mrow class="MJX-TeXAtom-ORD">
    <mfrac>
      <mrow>
        <mrow class="MJX-TeXAtom-ORD">
          <mi>x</mi>
          <mrow class="MJX-TeXAtom-ORD">
            <mo>/</mo>
          </mrow>
          <msup>
            <mi>y</mi>
            <mn>2</mn>
          </msup>
        </mrow>
        <mo>+</mo>
        <mn>2</mn>
        <mi>y</mi>
      </mrow>
      <mn>3</mn>
    </mfrac>
    <mo>.</mo>
  </mrow>
</math>

Use this formula to implement a cube-root procedure analogous to the
square-root procedure.  (In <a href="1_002e3.xhtml#g_t1_002e3_002e4">1.3.4</a> we will see how to implement
Newton’s method in general as an abstraction of these square-root and cube-root
procedures.)
</p></blockquote>

<a id="g_t1_002e1_002e8"></a>
<a id="Procedures-as-Black_002dBox-Abstractions"></a>
<h4 class="subsection"><span class="secnum">1.1.8</span><span class="sectitle">Procedures as Black-Box Abstractions</span></h4>

<p><code>Sqrt</code> is our first example of a process defined by a set of mutually
defined procedures.  Notice that the definition of <code>sqrt-iter</code> is
<a id="index-recursive-1"></a>
<em>recursive</em>; that is, the procedure is defined in terms of itself.  The
idea of being able to define a procedure in terms of itself may be disturbing;
it may seem unclear how such a “circular” definition could make sense at all,
much less specify a well-defined process to be carried out by a computer.  This
will be addressed more carefully in <a href="1_002e2.xhtml#g_t1_002e2">1.2</a>.  But first let’s
consider some other important points illustrated by the <code>sqrt</code> example.
</p>
<p>Observe that the problem of computing square roots breaks up naturally into a
number of subproblems: how to tell whether a guess is good enough, how to
improve a guess, and so on.  Each of these tasks is accomplished by a separate
procedure.  The entire <code>sqrt</code> program can be viewed as a cluster of
procedures (shown in <a href="#Figure-1_002e2">Figure 1.2</a>) that mirrors the decomposition of the
problem into subproblems.
</p>
<figure class="float">
<a id="Figure-1_002e2"></a>
<object style="width: 34.97ex; height: 15.63ex;" data="fig/chap1/Fig1.2.std.svg" type="image/svg+xml">SVG</object>

<figcaption class="float-caption">
<p><strong>Figure 1.2:</strong> Procedural decomposition of the <code>sqrt</code> program.</p>
</figcaption>
</figure>

<p>The importance of this decomposition strategy is not simply that one is
dividing the program into parts.  After all, we could take any large program
and divide it into parts—the first ten lines, the next ten lines, the next
ten lines, and so on.  Rather, it is crucial that each procedure accomplishes
an identifiable task that can be used as a module in defining other procedures.
For example, when we define the <code>good-enough?</code> procedure in terms of
<code>square</code>, we are able to regard the <code>square</code> procedure as a “black
box.”  We are not at that moment concerned with <em>how</em> the procedure
computes its result, only with the fact that it computes the square.  The
details of how the square is computed can be suppressed, to be considered at a
later time.  Indeed, as far as the <code>good-enough?</code> procedure is concerned,
<code>square</code> is not quite a procedure but rather an abstraction of a
procedure, a so-called <a id="index-procedural-abstraction"></a>
<em>procedural abstraction</em>.  At this level of
abstraction, any procedure that computes the square is equally good.
</p>
<p>Thus, considering only the values they return, the following two procedures for
squaring a number should be indistinguishable.  Each takes a numerical argument
and produces the square of that number as the value.<a class="footnote_link" id="DOCF25" href="#FOOT25"><sup>25</sup></a>
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square x</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> x x</span><span class="clo">))</span><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square x</span><span class="clo">)</span><span class="pln"> 
  </span><span class="opn">(</span><span class="pln">exp </span><span class="opn">(</span><span class="pln">double </span><span class="opn">(</span><span class="pln">log x</span><span class="clo">))))</span><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">double x</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">+</span><span class="pln"> x x</span><span class="clo">))</span></pre></div>

<p>So a procedure definition should be able to suppress detail.  The users of the
procedure may not have written the procedure themselves, but may have obtained
it from another programmer as a black box.  A user should not need to know how
the procedure is implemented in order to use it.
</p>
<a id="Local-names"></a>
<h5 class="subsubheading">Local names</h5>

<p>One detail of a procedure’s implementation that should not matter to the user
of the procedure is the implementer’s choice of names for the procedure’s
formal parameters.  Thus, the following procedures should not be
distinguishable:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square x</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> x x</span><span class="clo">))</span><span class="pln">
</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square y</span><span class="clo">)</span><span class="pln"> </span><span class="opn">(</span><span class="pun">*</span><span class="pln"> y y</span><span class="clo">))</span></pre></div>

<p>This principle—that the meaning of a procedure should be independent of the
parameter names used by its author—seems on the surface to be self-evident,
but its consequences are profound.  The simplest consequence is that the
parameter names of a procedure must be local to the body of the procedure.  For
example, we used <code>square</code> in the definition of <code>good-enough?</code> in our
square-root procedure:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square guess</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">))</span><span class="pln"> </span><span class="lit">0.001</span><span class="clo">))</span></pre></div>

<p>The intention of the author of <code>good-enough?</code> is to determine if the
square of the first argument is within a given tolerance of the second
argument.  We see that the author of <code>good-enough?</code> used the name
<code>guess</code> to refer to the first argument and <code>x</code> to refer to the second
argument.  The argument of <code>square</code> is <code>guess</code>.  If the author of
<code>square</code> used <code>x</code> (as above) to refer to that argument, we see that
the <code>x</code> in <code>good-enough?</code> must be a different <code>x</code> than the one
in <code>square</code>.  Running the procedure <code>square</code> must not affect the
value of <code>x</code> that is used by <code>good-enough?</code>, because that value of
<code>x</code> may be needed by <code>good-enough?</code> after <code>square</code> is done
computing.
</p>
<p>If the parameters were not local to the bodies of their respective procedures,
then the parameter <code>x</code> in <code>square</code> could be confused with the
parameter <code>x</code> in <code>good-enough?</code>, and the behavior of
<code>good-enough?</code> would depend upon which version of <code>square</code> we used.
Thus, <code>square</code> would not be the black box we desired.
</p>
<p>A formal parameter of a procedure has a very special role in the procedure
definition, in that it doesn’t matter what name the formal parameter has.  Such
a name is called a <a id="index-bound-variable"></a>
<em>bound variable</em>, and we say that the procedure
definition <a id="index-binds"></a>
<em>binds</em> its formal parameters.  The meaning of a procedure
definition is unchanged if a bound variable is consistently renamed throughout
the definition.<a class="footnote_link" id="DOCF26" href="#FOOT26"><sup>26</sup></a>  If a variable is not bound, we say that it is <a id="index-free"></a>
<em>free</em>.
The set of expressions for which a binding defines a name is called the
<a id="index-scope"></a>
<em>scope</em> of that name.  In a procedure definition, the bound variables
declared as the formal parameters of the procedure have the body of the
procedure as their scope.
</p>
<p>In the definition of <code>good-enough?</code> above, <code>guess</code> and <code>x</code> are
bound variables but <code>&lt;</code>, <code>-</code>, <code>abs</code>, and <code>square</code> are free.
The meaning of <code>good-enough?</code> should be independent of the names we choose
for <code>guess</code> and <code>x</code> so long as they are distinct and different from
<code>&lt;</code>, <code>-</code>, <code>abs</code>, and <code>square</code>.  (If we renamed <code>guess</code>
to <code>abs</code> we would have introduced a bug by <a id="index-capturing"></a>
<em>capturing</em> the
variable <code>abs</code>.  It would have changed from free to bound.)  The meaning
of <code>good-enough?</code> is not independent of the names of its free variables,
however.  It surely depends upon the fact (external to this definition) that
the symbol <code>abs</code> names a procedure for computing the absolute value of a
number.  <code>Good-enough?</code> will compute a different function if we substitute
<code>cos</code> for <code>abs</code> in its definition.
</p>
<a id="Internal-definitions-and-block-structure"></a>
<h5 class="subsubheading">Internal definitions and block structure</h5>

<p>We have one kind of name isolation available to us so far: The formal
parameters of a procedure are local to the body of the procedure.  The
square-root program illustrates another way in which we would like to control
the use of names.  The existing program consists of separate procedures:
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt x</span><span class="clo">)</span><span class="pln"> 
  </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="lit">1.0</span><span class="pln"> x</span><span class="clo">))</span><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt-iter guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
      guess
      </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">)))</span><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square guess</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">))</span><span class="pln"> </span><span class="lit">0.001</span><span class="clo">))</span><span class="pln">

</span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">average guess </span><span class="opn">(</span><span class="pun">/</span><span class="pln"> x guess</span><span class="clo">)))</span></pre></div>

<p>The problem with this program is that the only procedure that is important to
users of <code>sqrt</code> is <code>sqrt</code>.  The other procedures (<code>sqrt-iter</code>,
<code>good-enough?</code>, and <code>improve</code>) only clutter up their minds.  They may
not define any other procedure called <code>good-enough?</code> as part of another
program to work together with the square-root program, because <code>sqrt</code>
needs it.  The problem is especially severe in the construction of large
systems by many separate programmers.  For example, in the construction of a
large library of numerical procedures, many numerical functions are computed as
successive approximations and thus might have procedures named
<code>good-enough?</code> and <code>improve</code> as auxiliary procedures.  We would like
to localize the subprocedures, hiding them inside <code>sqrt</code> so that
<code>sqrt</code> could coexist with other successive approximations, each having its
own private <code>good-enough?</code> procedure.  To make this possible, we allow a
procedure to have internal definitions that are local to that procedure.  For
example, in the square-root problem we can write
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
    </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square guess</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">))</span><span class="pln"> </span><span class="lit">0.001</span><span class="clo">))</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln">
    </span><span class="opn">(</span><span class="pln">average guess </span><span class="opn">(</span><span class="pun">/</span><span class="pln"> x guess</span><span class="clo">)))</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt-iter guess x</span><span class="clo">)</span><span class="pln">
    </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess x</span><span class="clo">)</span><span class="pln">
        guess
        </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="opn">(</span><span class="pln">improve guess x</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">)))</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="lit">1.0</span><span class="pln"> x</span><span class="clo">))</span></pre></div>

<p>Such nesting of definitions, called <a id="index-block-structure"></a>
<em>block structure</em>, is basically the
right solution to the simplest name-packaging problem.  But there is a better
idea lurking here.  In addition to internalizing the definitions of the
auxiliary procedures, we can simplify them.  Since <code>x</code> is bound in the
definition of <code>sqrt</code>, the procedures <code>good-enough?</code>, <code>improve</code>,
and <code>sqrt-iter</code>, which are defined internally to <code>sqrt</code>, are in the
scope of <code>x</code>.  Thus, it is not necessary to pass <code>x</code> explicitly to
each of these procedures.  Instead, we allow <code>x</code> to be a free variable in
the internal definitions, as shown below. Then <code>x</code> gets its value from the
argument with which the enclosing procedure <code>sqrt</code> is called.  This
discipline is called <a id="index-lexical-scoping"></a>
<em>lexical scoping</em>.<a class="footnote_link" id="DOCF27" href="#FOOT27"><sup>27</sup></a>
</p>
<div class="lisp">
<pre class="lisp prettyprinted" style=""><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt x</span><span class="clo">)</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess</span><span class="clo">)</span><span class="pln">
    </span><span class="opn">(</span><span class="pun">&lt;</span><span class="pln"> </span><span class="opn">(</span><span class="pln">abs </span><span class="opn">(</span><span class="pun">-</span><span class="pln"> </span><span class="opn">(</span><span class="pln">square guess</span><span class="clo">)</span><span class="pln"> x</span><span class="clo">))</span><span class="pln"> </span><span class="lit">0.001</span><span class="clo">))</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">improve guess</span><span class="clo">)</span><span class="pln">
    </span><span class="opn">(</span><span class="pln">average guess </span><span class="opn">(</span><span class="pun">/</span><span class="pln"> x guess</span><span class="clo">)))</span><span class="pln">
  </span><span class="opn">(</span><span class="kwd">define</span><span class="pln"> </span><span class="opn">(</span><span class="pln">sqrt-iter guess</span><span class="clo">)</span><span class="pln">
    </span><span class="opn">(</span><span class="kwd">if</span><span class="pln"> </span><span class="opn">(</span><span class="pln">good-enough? guess</span><span class="clo">)</span><span class="pln">
        guess
        </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="opn">(</span><span class="pln">improve guess</span><span class="clo">))))</span><span class="pln">
  </span><span class="opn">(</span><span class="pln">sqrt-iter </span><span class="lit">1.0</span><span class="clo">))</span></pre></div>

<p>We will use block structure extensively to help us break up large
programs into tractable pieces.<a class="footnote_link" id="DOCF28" href="#FOOT28"><sup>28</sup></a>
The idea of block structure originated with the programming language
Algol 60. It appears in most advanced programming languages and is an
important tool for helping to organize the construction of large
programs.
</p>
<div class="footnote">
<h4 class="footnotes-heading">Footnotes</h4>

<div id="FOOT4"><p><a class="footnote_backlink" href="#DOCF4"><sup>4</sup></a>
The characterization of
numbers as “simple data” is a barefaced bluff.  In fact, the treatment of
numbers is one of the trickiest and most confusing aspects of any programming
language.  Some typical issues involved are these: Some computer systems
distinguish <a id="index-integers"></a>
<em>integers</em>, such as 2, from <a id="index-real-numbers"></a>
<em>real numbers</em>, such as
2.71.  Is the real number 2.00 different from the integer 2?  Are the
arithmetic operations used for integers the same as the operations used for
real numbers?  Does 6 divided by 2 produce 3, or 3.0?  How large a number can
we represent?  How many decimal places of accuracy can we represent?  Is the
range of integers the same as the range of real numbers?  Above and beyond
these questions, of course, lies a collection of issues concerning roundoff and
truncation errors—the entire science of numerical analysis.  Since our focus
in this book is on large-scale program design rather than on numerical
techniques, we are going to ignore these problems.  The numerical examples in
this chapter will exhibit the usual roundoff behavior that one observes when
using arithmetic operations that preserve a limited number of decimal places of
accuracy in noninteger operations.</p>
</div>
<div id="FOOT5"><p><a class="footnote_backlink" href="#DOCF5"><sup>5</sup></a>
Throughout this book, when
we wish to emphasize the distinction between the input typed by the user and
the response printed by the interpreter, we will show the latter in slanted
characters.</p>
</div>
<div id="FOOT6"><p><a class="footnote_backlink" href="#DOCF6"><sup>6</sup></a>
Lisp systems typically provide features to aid the user in
formatting expressions.  Two especially useful features are one that
automatically indents to the proper pretty-print position whenever a new line
is started and one that highlights the matching left parenthesis whenever a
right parenthesis is typed.</p>
</div>
<div id="FOOT7"><p><a class="footnote_backlink" href="#DOCF7"><sup>7</sup></a>
Lisp obeys the
convention that every expression has a value. This convention, together with
the old reputation of Lisp as an inefficient language, is the source of the
quip by Alan Perlis (paraphrasing Oscar Wilde) that “Lisp programmers know the
value of everything but the cost of nothing.”</p>
</div>
<div id="FOOT8"><p><a class="footnote_backlink" href="#DOCF8"><sup>8</sup></a>
In this book, we do not show the interpreter’s response
to evaluating definitions, since this is highly implementation-dependent.</p>
</div>
<div id="FOOT9"><p><a class="footnote_backlink" href="#DOCF9"><sup>9</sup></a>
<a href="Chapter-3.xhtml#Chapter-3">Chapter 3</a> will show that this notion of
environment is crucial, both for understanding how the interpreter works and
for implementing interpreters.</p>
</div>
<div id="FOOT10"><p><a class="footnote_backlink" href="#DOCF10"><sup>10</sup></a>
It may seem strange that
the evaluation rule says, as part of the first step, that we should evaluate
the leftmost element of a combination, since at this point that can only be an
operator such as <code>+</code> or <code>*</code> representing a built-in primitive
procedure such as addition or multiplication.  We will see later that it is
useful to be able to work with combinations whose operators are themselves
compound expressions.</p>
</div>
<div id="FOOT11"><p><a class="footnote_backlink" href="#DOCF11"><sup>11</sup></a>
Special syntactic forms that are simply convenient
alternative surface structures for things that can be written in more uniform
ways are sometimes called <a id="index-syntactic-sugar"></a>
<em>syntactic sugar</em>, to use a phrase coined by
Peter Landin.  In comparison with users of other languages, Lisp programmers,
as a rule, are less concerned with matters of syntax.  (By contrast, examine
any Pascal manual and notice how much of it is devoted to descriptions of
syntax.)  This disdain for syntax is due partly to the flexibility of Lisp,
which makes it easy to change surface syntax, and partly to the observation
that many “convenient” syntactic constructs, which make the language less
uniform, end up causing more trouble than they are worth when programs become
large and complex.  In the words of Alan Perlis, “Syntactic sugar causes
cancer of the semicolon.”</p>
</div>
<div id="FOOT12"><p><a class="footnote_backlink" href="#DOCF12"><sup>12</sup></a>
Observe that there are two different operations being
combined here: we are creating the procedure, and we are giving it the name
<code>square</code>.  It is possible, indeed important, to be able to separate these
two notions—to create procedures without naming them, and to give names to
procedures that have already been created.  We will see how to do this in
<a href="1_002e3.xhtml#g_t1_002e3_002e2">1.3.2</a>.</p>
</div>
<div id="FOOT13"><p><a class="footnote_backlink" href="#DOCF13"><sup>13</sup></a>
Throughout this book, we will describe the general
syntax of expressions by using italic symbols delimited by angle
brackets—e.g., <code>⟨</code><var>name</var><code>⟩</code>—to denote the “slots” in the expression to be
filled in when such an expression is actually used.</p>
</div>
<div id="FOOT14"><p><a class="footnote_backlink" href="#DOCF14"><sup>14</sup></a>
More generally, the body of the procedure can be a
sequence of expressions.  In this case, the interpreter evaluates each
expression in the sequence in turn and returns the value of the final
expression as the value of the procedure application.</p>
</div>
<div id="FOOT15"><p><a class="footnote_backlink" href="#DOCF15"><sup>15</sup></a>
Despite the simplicity of the substitution idea, it turns
out to be surprisingly complicated to give a rigorous mathematical definition
of the substitution process.  The problem arises from the possibility of
confusion between the names used for the formal parameters of a procedure and
the (possibly identical) names used in the expressions to which the procedure
may be applied.  Indeed, there is a long history of erroneous definitions of
<a id="index-substitution"></a>
<em>substitution</em> in the literature of logic and programming semantics.
See <a href="References.xhtml#Stoy-1977">Stoy 1977</a> for a careful discussion of substitution.</p>
</div>
<div id="FOOT16"><p><a class="footnote_backlink" href="#DOCF16"><sup>16</sup></a>
In <a href="Chapter-3.xhtml#Chapter-3">Chapter 3</a> we will introduce
<a id="index-stream-processing"></a>
<em>stream processing</em>, which is a way of handling apparently “infinite”
data structures by incorporating a limited form of normal-order evaluation.  In
<a href="4_002e2.xhtml#g_t4_002e2">4.2</a> we will modify the Scheme interpreter to produce a
normal-order variant of Scheme.</p>
</div>
<div id="FOOT17"><p><a class="footnote_backlink" href="#DOCF17"><sup>17</sup></a>
“Interpreted as either true or false” means
this: In Scheme, there are two distinguished values that are denoted by the
constants <code>#t</code> and <code>#f</code>.  When the interpreter checks a predicate’s
value, it interprets <code>#f</code> as false.  Any other value is treated as true.
(Thus, providing <code>#t</code> is logically unnecessary, but it is convenient.)  In
this book we will use names <code>true</code> and <code>false</code>, which are associated
with the values <code>#t</code> and <code>#f</code> respectively.</p>
</div>
<div id="FOOT18"><p><a class="footnote_backlink" href="#DOCF18"><sup>18</sup></a>
<code>Abs</code> also uses the “minus” operator <code>-</code>,
which, when used with a single operand, as in <code>(- x)</code>, indicates
negation.</p>
</div>
<div id="FOOT19"><p><a class="footnote_backlink" href="#DOCF19"><sup>19</sup></a>
A minor difference between <code>if</code> and <code>cond</code> is
that the <code>⟨</code><var>e</var><code>⟩</code> part of each <code>cond</code> clause may be a sequence of
expressions.  If the corresponding <code>⟨</code><var>p</var><code>⟩</code> is found to be true, the
expressions <code>⟨</code><var>e</var><code>⟩</code> are evaluated in sequence and the value of the final
expression in the sequence is returned as the value of the <code>cond</code>.  In an
<code>if</code> expression, however, the <code>⟨</code><var>consequent</var><code>⟩</code> and <code>⟨</code><var>alternative</var><code>⟩</code>
must be single expressions.</p>
</div>
<div id="FOOT20"><p><a class="footnote_backlink" href="#DOCF20"><sup>20</sup></a>
Declarative
and imperative descriptions are intimately related, as indeed are mathematics
and computer science.  For instance, to say that the answer produced by a
program is “correct” is to make a declarative statement about the program.
There is a large amount of research aimed at establishing techniques for
proving that programs are correct, and much of the technical difficulty of this
subject has to do with negotiating the transition between imperative statements
(from which programs are constructed) and declarative statements (which can be
used to deduce things).  In a related vein, an important current area in
programming-language design is the exploration of so-called very high-level
languages, in which one actually programs in terms of declarative statements.
The idea is to make interpreters sophisticated enough so that, given “what
is” knowledge specified by the programmer, they can generate “how to”
knowledge automatically.  This cannot be done in general, but there are
important areas where progress has been made.  We shall revisit this idea in
<a href="Chapter-4.xhtml#Chapter-4">Chapter 4</a>.</p>
</div>
<div id="FOOT21"><p><a class="footnote_backlink" href="#DOCF21"><sup>21</sup></a>
This square-root algorithm
is actually a special case of Newton’s method, which is a general technique for
finding roots of equations.  The square-root algorithm itself was developed by
Heron of Alexandria in the first century <abbr>A.D.</abbr>  We will see how to
express the general Newton’s method as a Lisp procedure in <a href="1_002e3.xhtml#g_t1_002e3_002e4">1.3.4</a>.</p>
</div>
<div id="FOOT22"><p><a class="footnote_backlink" href="#DOCF22"><sup>22</sup></a>
We will usually give predicates
names ending with question marks, to help us remember that they are predicates.
This is just a stylistic convention.  As far as the interpreter is concerned,
the question mark is just an ordinary character.</p>
</div>
<div id="FOOT23"><p><a class="footnote_backlink" href="#DOCF23"><sup>23</sup></a>
Observe that we express our
initial guess as 1.0 rather than 1.  This would not make any difference in many
Lisp implementations.  <abbr>MIT</abbr> Scheme, however, distinguishes between
exact integers and decimal values, and dividing two integers produces a
rational number rather than a decimal.  For example, dividing 10 by 6 yields
5/3, while dividing 10.0 by 6.0 yields 1.6666666666666667.  (We will learn how
to implement arithmetic on rational numbers in <a href="2_002e1.xhtml#g_t2_002e1_002e1">2.1.1</a>.)  If we
start with an initial guess of 1 in our square-root program, and <math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>x</mi>
</math> is an
exact integer, all subsequent values produced in the square-root computation
will be rational numbers rather than decimals.  Mixed operations on rational
numbers and decimals always yield decimals, so starting with an initial guess
of 1.0 forces all subsequent values to be decimals.</p>
</div>
<div id="FOOT24"><p><a class="footnote_backlink" href="#DOCF24"><sup>24</sup></a>
Readers
who are worried about the efficiency issues involved in using procedure calls
to implement iteration should note the remarks on “tail recursion” in 
<a href="1_002e2.xhtml#g_t1_002e2_002e1">1.2.1</a>.</p>
</div>
<div id="FOOT25"><p><a class="footnote_backlink" href="#DOCF25"><sup>25</sup></a>
It is not even
clear which of these procedures is a more efficient implementation.  This
depends upon the hardware available.  There are machines for which the
“obvious” implementation is the less efficient one.  Consider a machine that
has extensive tables of logarithms and antilogarithms stored in a very
efficient manner.</p>
</div>
<div id="FOOT26"><p><a class="footnote_backlink" href="#DOCF26"><sup>26</sup></a>
The concept of consistent renaming is actually subtle
and difficult to define formally.  Famous logicians have made embarrassing
errors here.</p>
</div>
<div id="FOOT27"><p><a class="footnote_backlink" href="#DOCF27"><sup>27</sup></a>
Lexical scoping dictates 
that free variables in a procedure are taken to refer to bindings made by enclosing 
procedure definitions; that is, they are looked up in the environment in which the
procedure was defined. We will see how this works in detail in chapter 3 when we study 
environments and the detailed behavior of the interpreter.<a id="Footnote-28"></a></p>
</div>
<div id="FOOT28"><p><a class="footnote_backlink" href="#DOCF28"><sup>28</sup></a>
Embedded definitions must come
first in a procedure body. The management is not responsible for the
consequences of running programs that intertwine definition and use.</p>
</div>
</div>
<nav class="header">
<p>
Next: <a href="1_002e2.xhtml#g_t1_002e2" accesskey="n" rel="next">1.2</a>, Prev: <a href="Chapter-1.xhtml#Chapter-1" accesskey="p" rel="prev">Chapter 1</a>, Up: <a href="#g_t1_002e1" accesskey="u" rel="prev">1.1</a>   [<a href="index.xhtml#SEC_Contents" title="Table of contents" accesskey="c" rel="contents">Contents</a>]</p>
</nav>


</section><span class="bottom jump" title="Jump to bottom"><a href="#pagebottom" accesskey="b">⇣</a></span><a id="pagebottom"></a>
</body>
</html>